package ferp.android.widgets.buttons;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.widgets.BidView;
import ferp.core.game.Game;
import ferp.core.mode.Mode;
import ferp.core.player.Profile;

/**
 * User: igorgok
 * Date: 12/30/13 12:25 PM
 */
public class ResumeGameButton extends TwoLayerButton /*implements Mode.Viewer*/
{
  public interface Reference
  {
    float getTextHeight();
  }

  public ResumeGameButton(Context context) {this(context, null, 0);}
  public ResumeGameButton(Context context, AttributeSet attrs) {this(context, attrs, 0);}
  public ResumeGameButton(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);

    caption.setGravity(Gravity.CENTER);
    caption.setTypeface(GUI.Font.BOLD);
    caption.setTextColor(new ColorStateList(new int[][]{
                                                new int[]{android.R.attr.state_enabled},
                                                new int[]{-android.R.attr.state_enabled},
                                            },
                                            new int[]{
                                                0xff68007c, //0xffb71400,
                                                GUI.Color.DISABLE,
                                            }));
    caption.setText(R.string.entrance_resume_game);

    flavor.setGravity(Gravity.CENTER);
    flavor.setTypeface(GUI.Font.BOLD);
    flavor.setTextColor(Color.BLACK);

    title.setGravity(Gravity.CENTER);
    title.setTypeface(GUI.Font.BOLD);
    title.setTextColor(Color.BLACK);
  }

  public void set(Reference reference) {this.reference = reference;}

  // set game information
  public final void update(Profile p)
  {
    boolean enable = p != null && p.hasGame();

    setEnabled(enable);

    if (enable)
    {
      Game g = p.game();
      Mode m = g.mode();

      title.setVisibility(GONE);
      contract.setVisibility(GONE);

      // set game flavor
      flavor.setText(getResources().getStringArray(R.array.pool_types)[p.settings.gameFlavor.ordinal()] + " | ");

      // set current contract (if available)
/*
      if (m != null)
        m.show(this, p);
*/
    }

    info.setVisibility(enable ? VISIBLE : GONE);
    caption.setEnabled(enable);
  }

/*
  @Override
  public void bidding()
  {
    title.setText(R.string.term_bidding);
    title.setVisibility(VISIBLE);
  }

  @Override
  public void dropping()
  {
    title.setText(R.string.term_dropping);
    title.setVisibility(VISIBLE);
  }

  @Override
  public void contracting()
  {
    title.setText(R.string.term_contracting);
    title.setVisibility(VISIBLE);
  }

  @Override
  public void playing(Bid bid)
  {
    contract.show(bid);
    contract.setVisibility(VISIBLE);
  }

  @Override
  public void passing(int factor)
  {
    //title.setText(GUI.getColoredText(getContext().getString(R.string.term_allpass), "red", " ×" + factor));
    title.setText(GUI.getColoredText(getContext().getString(R.string.term_allpass), "#b71400", " ×" + factor));
    //title.setText(getContext().getString(R.string.term_allpass) + " ×" + factor);
    title.setVisibility(VISIBLE);
  }

  @Override
  public void misere()
  {
    title.setText(R.string.misere);
    title.setVisibility(VISIBLE);
  }

  @Override
  public void unknown()
  {
    title.setText("?");
    title.setVisibility(VISIBLE);
  }
*/

  @Override
  protected final View onCreateUpperLayer(Context context, AttributeSet attrs)
  {
    LinearLayout main = new LinearLayout(context);
    LinearLayout.LayoutParams clp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams ilp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams elp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    clp.gravity = ilp.gravity = elp.gravity = Gravity.CENTER;

    info = new LinearLayout(context);

    info.setOrientation(LinearLayout.HORIZONTAL);
    info.addView(flavor = new TextView(context), elp);
    info.addView(title = new TextView(context), elp);
    info.addView(contract = new BidView(context, 0.75f, 0.75f), elp);

    main.setOrientation(LinearLayout.VERTICAL);
    main.addView(caption = new TextView(context), clp);
    main.addView(info, ilp);

    return main;
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    // measure only caption
    //caption.measure(widthMeasureSpec, heightMeasureSpec);

    if (reference != null)
    {
      float sc = reference.getTextHeight(), si = sc * 0.8f;

      caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, sc);
      flavor.setTextSize(TypedValue.COMPLEX_UNIT_PX, si);
      title.setTextSize(TypedValue.COMPLEX_UNIT_PX, si);
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  @Override
  protected int getButtonBackgroundResource()
  {
    return R.drawable.selector_button_intro;
  }

  protected int getUpperLayerLayoutWidth() {return ViewGroup.LayoutParams.WRAP_CONTENT;}
  protected int getUpperLayerLayoutHeight() {return ViewGroup.LayoutParams.WRAP_CONTENT;}

  // text view
  private TextView caption;
  private LinearLayout info;
  private TextView flavor;
  private BidView contract;
  private TextView title;

  private Reference reference;
}
